//
//  ShouKuanViewController.m
//  wangzhe
//
//  Created by 李 佳伟 on 2017/11/22.
//  Copyright © 2017年 李 佳伟. All rights reserved.
//

#import "ShouKuanViewController.h"
#import "JTCSM.h"
#import "SettingMoneyViewController.h"
#import "MyInfoView.h"

@interface ShouKuanViewController ()<SettingMoneyViewControllerdelegate>
{
    UILabel *_label;
    UIImageView *erweimaIamgeV;
    UILabel *moneyLabel;
    
    UIButton *jinerBtn;
    UIButton *saveImageBtn;
    
    NSString *encrypt;
    
    UIImage *tempImage;
    
    CGFloat backviewWidth;
    
    CGFloat iamgeviewsize;
    CGFloat thechangeSpace;
    CGFloat viewspace;
    CGFloat labelheight;
    CGFloat bottomHeight;
    CGFloat imagesize;
    CGFloat bottomViewHeight;
    
    
}
@property (nonatomic,strong) UIView *line;
@property (nonatomic,strong) UIView *backview;
@property (nonatomic,strong) UILabel *zhifuTypeLabel;
@property (nonatomic,strong) UILabel *changeLabel;
@property (nonatomic,strong) UIView *changeLineView;
@property (nonatomic,strong) UIView *secondView;
@property (nonatomic,strong) MyInfoView *infoView;
@property (nonatomic,strong) UIView *bottomview;
@property (nonatomic,strong) UIView *oneLabel;
@property (nonatomic,strong) UIView *twoLabel;
@property (nonatomic,strong) UIView *threeLabel;
@end

@implementation ShouKuanViewController

- (void)gotobackVC{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = KZhuTiRGB;
    self.navigationController.navigationBar.hidden = YES;
    [df isRemoveTarbar:YES];
    _label = [UILabel initWithFrame:CGRectMake(0, 20, KSCREENWidth, 44) text:@"收款" font:22 textAlignment:NSTextAlignmentCenter texcolor:RGBACOLOR(51, 51, 51, 1) backgroundColor:[UIColor clearColor]];
    [self.view addSubview:_label];
    
    UIButton *button = [[UIButton alloc] init];
    button.frame = CGRectMake(10, 20, 60, 44);
    [button setImage:[UIImage imageNamed:@"首页_扫码收款_返回键.png"] forState:UIControlStateNormal];
    button.imageEdgeInsets = UIEdgeInsetsMake(12.5, 5, 12.5, 43);
    button.titleEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0);
    [button addTarget:self action:@selector(gotobackVC) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:RGBACOLOR(51, 51, 51, 1) forState:UIControlStateNormal];
    [self.view addSubview:button];
    iamgeviewsize    = 318*[JTCSMSingle shareSingle].adaptationNumber;
    thechangeSpace   = 30*[JTCSMSingle shareSingle].adaptationNumber;
    viewspace        = 20;
    labelheight      = 50*[JTCSMSingle shareSingle].adaptationNumber;
    bottomHeight     = 50*[JTCSMSingle shareSingle].adaptationNumber;
    imagesize        = 155*[JTCSMSingle shareSingle].adaptationNumber;
    bottomViewHeight = 105*[JTCSMSingle shareSingle].adaptationNumber;
    [self layoutSubeview];

}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)layoutSubeview{
    
    backviewWidth =KCONTROLLERWith-2*viewspace;
    _backview = [[UIView alloc]initWithFrame:CGRectMake(viewspace, 80, backviewWidth, iamgeviewsize)];
    _backview.backgroundColor = [UIColor whiteColor];
    _backview.userInteractionEnabled = YES;
    [self.view addSubview:_backview];
    [df layerWithView:_backview radius:5 width:0 color:nil];
    
//    [df setTheViewYiYing:backview shadowColor:RGBACOLOR(89, 89, 89, 1) shadowOffset:CGSizeMake(3, 3) apla:.5];
    
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _backview.LC_width, labelheight)];
    headerView.backgroundColor = RGBACOLOR(241, 241, 241, 1);
    [_backview addSubview:headerView];
    CGFloat headerimagesize = 15;
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(10, (labelheight-headerimagesize)/2, headerimagesize, headerimagesize)];
    imageview.image = [UIImage imageNamed:@"首页_扫码收款_收款图标.png"];
    [_backview addSubview:imageview];
    
    UILabel *titleLabel = [UILabel initWithFrame:CGRectMake(KVIEW_X(imageview)+10, 0, backviewWidth, labelheight) text:@"二维码收款" font:14 textAlignment:NSTextAlignmentLeft texcolor:RGBACOLOR(51, 51, 51, 1) backgroundColor:nil];
    [headerView addSubview:titleLabel];
    
    self.zhifuTypeLabel = [UILabel initWithFrame:CGRectZero text:@"" font:15 textAlignment:NSTextAlignmentCenter texcolor:RGBACOLOR(51, 51, 51, 1) backgroundColor:nil];
    [_backview addSubview:self.zhifuTypeLabel];
    
    self.changeLabel = [UILabel initWithFrame:CGRectZero text:@"更多" font:15 textAlignment:NSTextAlignmentLeft texcolor:[df colorWithHexString:@"ffbb18" alpha:1] backgroundColor:nil];
    [_backview addSubview:self.changeLabel];
    self.changeLineView = [[UIView alloc]initWithFrame:CGRectZero];
    self.changeLineView.backgroundColor = [df colorWithHexString:@"ffbb18" alpha:1];
    [_backview addSubview:self.changeLineView];
    
    erweimaIamgeV = [[UIImageView alloc]initWithFrame:CGRectMake((backviewWidth-imagesize)/2, _zhifuTypeLabel.LC_height+labelheight+thechangeSpace, imagesize, imagesize)];
    erweimaIamgeV.image = [UIImage  imageNamed:@"erweima.jpg"];
    [_backview addSubview:erweimaIamgeV];
    
    moneyLabel= [UILabel initWithFrame:CGRectZero text:@"¥0 " font:29 textAlignment:NSTextAlignmentCenter texcolor:RGBACOLOR(51, 51, 51, 1) backgroundColor:nil];
    [_backview addSubview:moneyLabel];

    
    jinerBtn = [[UIButton alloc] init];
    jinerBtn.frame = CGRectMake(0, KVIEW_Y(erweimaIamgeV)+20+moneyLabel.LC_height, backviewWidth/2-0.5, bottomHeight);
    [jinerBtn setTitle:@"设置金额" forState:UIControlStateNormal];
    [jinerBtn setTitleColor:[df colorWithHexString:@"333333" alpha:1] forState:UIControlStateNormal];
    jinerBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [jinerBtn addTarget:self action:@selector(gotoSettingMoneyVC) forControlEvents:UIControlEventTouchUpInside];
    [_backview addSubview:jinerBtn];
    
    
    _line = [[UIView alloc]initWithFrame:CGRectMake(backviewWidth/2, KVIEW_Y(erweimaIamgeV)+20+moneyLabel.LC_height+5, 1, bottomHeight-10)];
    _line.backgroundColor = [df colorWithHexString:@"999999" alpha:1];
    [_backview addSubview:_line];
    
    
    saveImageBtn = [[UIButton alloc] init];
    saveImageBtn.frame = CGRectMake(backviewWidth/2+0.5, KVIEW_Y(erweimaIamgeV)+20+moneyLabel.LC_height, backviewWidth/2-0.5, bottomHeight);
    [saveImageBtn setTitle:@"保存收款码" forState:UIControlStateNormal];
    [saveImageBtn setTitleColor:[df colorWithHexString:@"333333" alpha:1] forState:UIControlStateNormal];
    saveImageBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [saveImageBtn addTarget:self action:@selector(saveErweima) forControlEvents:UIControlEventTouchUpInside];
    [_backview addSubview:saveImageBtn];
    
    _secondView = [[UIView alloc]initWithFrame:CGRectMake(viewspace, KVIEW_Y(_backview)+10, (KCONTROLLERWith-2*viewspace), labelheight)];
    _secondView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_secondView];
    [df layerWithView:_secondView radius:5 width:0 color:nil];
    
    _infoView = [[MyInfoView alloc]initWithFrame:CGRectMake(10, 0, _secondView.LC_width, labelheight) imagename:nil title:@"银联云闪付" placeholder:nil isShowLabel:ImageViewType radioBtnIndex:0 GroupId:0];
    [_secondView addSubview:_infoView];
    
    
    _bottomview = [[UIView alloc]initWithFrame:CGRectMake(viewspace, KVIEW_Y(_secondView)+10, (KCONTROLLERWith-2*viewspace), bottomViewHeight)];
    _bottomview.backgroundColor = RGBACOLOR(255, 255, 255, 0.5);
    [self.view addSubview:_bottomview];
    [df layerWithView:_bottomview radius:5 width:0 color:nil];

    CGFloat fontsize = (KSCREENWidth == 320?10:(KSCREENWidth==375?12:14));
    _oneLabel = [UILabel initWithFrame:CGRectMake(10, 0, _bottomview.LC_width-20, _bottomview.LC_height/3) text:@"到账日期：当日交易，次日结算（节假日顺延）" font:fontsize textAlignment:NSTextAlignmentLeft texcolor:RGBACOLOR(51, 51, 51, 1) backgroundColor:nil];
    [_bottomview addSubview:_oneLabel];
    
    _twoLabel = [UILabel initWithFrame:CGRectMake(10, _bottomview.LC_height/3, _bottomview.LC_width-20, _bottomview.LC_height/3) text:@"结算费率：0.5%" font:fontsize textAlignment:NSTextAlignmentLeft texcolor:RGBACOLOR(51, 51, 51, 1) backgroundColor:nil];
    [_bottomview addSubview:_twoLabel];
    
    _threeLabel = [UILabel initWithFrame:CGRectMake(10, _bottomview.LC_height*2/3, _bottomview.LC_width-20, _bottomview.LC_height/3) text:@"收款方式：微信、支付宝、QQ钱包、京东钱包" font:fontsize textAlignment:NSTextAlignmentLeft texcolor:RGBACOLOR(51, 51, 51, 1) backgroundColor:nil];
    [_bottomview addSubview:_threeLabel];
    
    
    
}

#pragma mark change支付方式Frame
- (void)changeFrameWithLabelText:(NSString *)text money:(NSString *)money{
    CGRect rect = ([text isEqualToString:@""] || text == nil)?CGRectZero:[df labelHeightWithContent:text width:1000 font:15];
    CGRect rect1 = ([text isEqualToString:@""] || text == nil)?CGRectZero:[df labelHeightWithContent:@"更多" width:1000 font:15];
     CGRect rect2 = ([money isEqualToString:@""] || money == nil)?CGRectZero:[df labelHeightWithContent:money width:1000 font:29];
    _backview.frame = CGRectMake(viewspace, 80, backviewWidth, iamgeviewsize+rect.size.height+rect2.size.height);
    CGFloat changespace = (_backview.LC_width-rect.size.width-rect1.size.width-5)/2;
    self.zhifuTypeLabel.text = text;
    self.zhifuTypeLabel.frame = CGRectMake(changespace, labelheight+thechangeSpace/2, rect.size.width, rect.size.height);
    self.changeLabel.frame = CGRectMake(KVIEW_X(self.zhifuTypeLabel)+5, labelheight+thechangeSpace/2, rect1.size.width, rect.size.height);
    self.changeLineView.frame = CGRectMake(self.changeLabel.LC_x, self.changeLabel.LC_y+self.changeLabel.LC_height, self.changeLabel.LC_width, 1);
    erweimaIamgeV.frame = CGRectMake((backviewWidth-imagesize)/2, _zhifuTypeLabel.LC_height+80*[JTCSMSingle shareSingle].adaptationNumber, imagesize, imagesize);
    moneyLabel.text = [NSString stringWithFormat:@"¥%@",money];
    moneyLabel.frame = CGRectMake(0, KVIEW_Y(erweimaIamgeV)+10, backviewWidth, rect2.size.height);
    
    jinerBtn.frame = CGRectMake(0, KVIEW_Y(erweimaIamgeV)+20+moneyLabel.LC_height, backviewWidth/2-0.5, bottomHeight);
    saveImageBtn.frame = CGRectMake(backviewWidth/2+0.5, KVIEW_Y(erweimaIamgeV)+20+moneyLabel.LC_height, backviewWidth/2-0.5, bottomHeight);
    _line.frame = CGRectMake(backviewWidth/2, KVIEW_Y(erweimaIamgeV)+20+moneyLabel.LC_height+5, 1, bottomHeight-10);
    _secondView.frame = CGRectMake(viewspace, KVIEW_Y(_backview)+10, _backview.LC_width, labelheight);
    _bottomview.frame = CGRectMake(viewspace, KVIEW_Y(_secondView)+10, _backview.LC_width, bottomViewHeight);

}

- (void)gotoSettingMoneyVC{
    SettingMoneyViewController *vc = [[SettingMoneyViewController alloc]init];
    vc.nameTitle = @"设置金额";
    vc.encrypt = encrypt;
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
    [self changeFrameWithLabelText:@"微信支付" money:@"600"];
}

#pragma mark settingDelegate
- (void)getMoney:(NSString *)money erweima:(NSString *)imageurl backImage:(NSString *)backimageurl{
    
    [SDWebImageManager.sharedManager downloadImageWithURL:[NSURL URLWithString:backimageurl] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        if (!error) {
            tempImage = image;
            
        }
    }];
    moneyLabel.text = [NSString stringWithFormat:@"¥%@",money];
    

}

- (void)saveErweima{
    
//    [self loadImageFinished:tempImage];
    [self changeFrameWithLabelText:@"" money:@""];
}


- (void)loadImageFinished:(UIImage *)image
{
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    
    NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
    if (error) {
        [df showAlertWithTitle:@"保存失败！" message:@"请重新保存 "btn1:@"知道了" btn2:nil btn3:nil delegate:nil tag:0];
    }else{
        [df showAlertWithTitle:@"保存成功！" message:@"请打开本地相册进行扫码支付"btn1:@"知道了" btn2:@"" btn3:nil delegate:self tag:123];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
